package me.youline.dataServer.service.support.file;

import me.youline.dataServer.service.support.file.FileBuilder.FileEntity;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * ar用户编辑内容定位器
 * @author linhan
 *
 */
@Component
public class ContentFileLocator extends DefaultFileLocator implements FileLocator,NewFileLocatable<FileLocationConfig>{
	
	@Value("#{frontProperties['resource.fileCategory.content']}")
	private String categoryName = "content";
	
	public String getFilePath(String url){
		return this.getFileRoot() + url;
	}

	@Override
	public FileEntity getNewFileEntity(FileLocationConfig config) {
		DateTime time = new DateTime();
		Md5Hash file = new Md5Hash(String.valueOf(time.getMillis()));
		return FileBuilder.newInstance(config.getInput())
				.root(this.getFileRoot())
				.dir(String.valueOf(time.getYear()))
				.dir(String.valueOf(time.getMonthOfYear())).fileSize(config.getFileSize())
				.dir(String.valueOf(time.getDayOfMonth()))
				.file(file.toString()).getEntity();
	}


	@Override
	protected String getFileCategoryName() {
		return categoryName;
	}




}
